/**
 * Copyright (C) 2010 Hamburg Sud
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hamburgsud.log4testing.core.data;

import java.util.ArrayList;

/**
 * Represents a Group of TestSteps which could be identified by the name
 *  
 * @author 
 *
 */
public class TestStepGroup extends TestStepContainer {

	/**
	 * Array which contains all TestStesp
	 */
	private ArrayList<TestStep> testSteps = new ArrayList<TestStep>();

	/**
	 * Calls the super constructor
	 * @param name name of the TestStepGroup
	 */
	public TestStepGroup(String name) {
		super(name);
	}

	/**
	 * Returns the number of TestSteps
	 */
	public int getNumberOfTestSteps() {
		return testSteps.size();
	}

	/**
	 * Returns the number of Failed TestSteps
	 */
	public int getNumberOfFailed() {
		int numberOfFailed = 0;
		for (TestStep testStep : testSteps) {
			if (testStep.isFailed()) {
				++numberOfFailed;
			}
		}
		return numberOfFailed;
	}

	/**
	 * Adds a TestStep to the Array of TestSteps
	 * @param testStep TestStep to add
	 */
	public TestStepGroup addTestStep(final TestStep testStep) {

		testSteps.add(testStep);
		return this;
	}

	/**
	 * Returns an iterable representation of the <code> testSteps </code>
	 * @return iterable testSteps
	 */
	public Iterable<TestStep> getTestSteps() {
		return testSteps;
	}

	/**
	 * Returns the first Failed TestStep
	 */
	@Override
	public TestStep getFirstFailed() {
		TestStep firstFailed = null;
		for (TestStep testStep : testSteps) {
			if (testStep.isFailed()) {
				firstFailed = testStep;
				break;
			}
		}
		return firstFailed;
	}

	/**
	 * Returns the first TestStep
	 */
	@Override
	public TestStep getFirstTestStep() {
		return getTestStep(0);
	}

	/**
	 * Returns the last TestStep
	 */
	@Override
	public TestStep getLastTestStep() {
		return getTestStep(testSteps.size() - 1);
	}

	/**
	 * Returns the TestStep at the given position
	 * @param index the position of the TestStep
	 * @return TestStep at the Position or <code> null </code> if not found
	 */
	private TestStep getTestStep(int index) {
		if (testSteps.size() > index && index >= 0) {
			return testSteps.get(index);
		} else {
			return null;
		}
	}

}
